package com.example.sls.dao.field;

import com.example.sls.pojo.field.PromoCodes;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 优惠代码表(PromoCodes)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:07
 */
public interface PromoCodesDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from promo_codes where id=#{id}")
  PromoCodes queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param promoCodes 查询条件
   * @param pageSize   分页对象
   * @param offset     偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from promo_codes where 1=1" +
          " and (id=#{promoCodes.id} or (isnull(#{promoCodes.id}) or #{promoCodes.id}=0))" +
          " and (code=#{promoCodes.code} or (isnull(#{promoCodes.code}) or #{promoCodes.code}=''))" +
          " and (description=#{promoCodes.description} or (isnull(#{promoCodes.description}) or #{promoCodes.description}=''))" +
          " and (expiration_date=#{promoCodes.expirationDate} or (isnull(#{promoCodes.expirationDate}) or #{promoCodes.expirationDate}=''))" +
          " and (created_at=#{promoCodes.createdAt} or (isnull(#{promoCodes.createdAt}) or #{promoCodes.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<PromoCodes> queryAllByLimit(PromoCodes promoCodes, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from promo_codes")
  long count();

  /**
   * 新增数据
   *
   * @param promoCodes 实例对象
   * @return 影响行数
   */
  @Insert("insert into promo_codes (id,code,description,expiration_date,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{code}!='' and !isnull(#{code}),#{code},null)," +
          "if(#{description}!='' and !isnull(#{description}),#{description},null)," +
          "if(#{expirationDate}!='' and !isnull(#{expirationDate}),#{expirationDate},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(PromoCodes promoCodes);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param promoCodes 实例对象
   * @return 影响行数
   */
  @Update("update promo_codes set " +
          "code=if(isnull(#{code}) or #{code}='',code,#{code})," +
          "description=if(isnull(#{description}) or #{description}='',description,#{description})," +
          "expiration_date=if(isnull(#{expirationDate}) or #{expirationDate}='',expiration_date,#{expirationDate})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(PromoCodes promoCodes);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from promo_codes where id=#{id}")
  int deleteById(Integer id);
}

